//
//  HLIMKitInputBar.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitInputBar.h"
#import "HLIMKitAction.h"

@interface HLIMKitInputBar ()


@property (strong, nonatomic) NSMutableArray *barViews;
@property (assign, nonatomic) HLIMInputStatus status;
@property (copy,    nonatomic) NSString *quote;

@end

@implementation HLIMKitInputBar


- (instancetype)initWithFrame:(CGRect)frame delegate:(nonnull id<HLIMKitInputBarDelegate>)delegate
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.delegate = delegate;
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI
{
    self.backgroundColor = UIColor.whiteColor;
    
    self.bg = [UIView.alloc initWithFrame:CGRectMake(16, 12, self.width - 32, 52)];
    self.bg.layer.cornerRadius = 26;
    self.bg.layer.masksToBounds = YES;
    self.bg.backgroundColor = UIColor.whiteColor;
    [self addSubview:self.bg];
    
    _inputTextView = [[NIMGrowingTextView alloc] initWithFrame:CGRectMake(48, 6, self.width - 48 - 76, 32)];
    _inputTextView.backgroundColor = HLBackgroundColor;
    _inputTextView.layer.cornerRadius = 16;
    _inputTextView.layer.masksToBounds = YES;
    _inputTextView.font = [UIFont systemFontOfSize:12.0f];
    _inputTextView.maxNumberOfLines = 4;
    _inputTextView.minNumberOfLines = 1;
    _inputTextView.placeholderAttributedText = [NSAttributedString.alloc initWithString:@"请输入信息..." attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:174/255.0 green:178/255.0 blue:185/255.0 alpha:1.0], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    _inputTextView.textColor = [UIColor colorWithRed:26/255.0 green:26/255.0 blue:26/255.0 alpha:1.0];
    _inputTextView.textViewDelegate = self;
    _inputTextView.returnKeyType = UIReturnKeySend;
    _inputTextView.textContainerInset = UIEdgeInsetsMake(8, 12,  8, 5);
    [self addSubview:_inputTextView];
    
    _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendButton.viewSize = CGSizeMake(52, 32);
    _sendButton.right = self.width - 16;
    _sendButton.centerY = _inputTextView.centerY;
    [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [_sendButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    _sendButton.titleLabel.font = [UIFont systemFontOfSize:12];
    _sendButton.backgroundColor = HLMainColor;
    _sendButton.layer.cornerRadius = 16;
    _sendButton.layer.masksToBounds = YES;
    [self addSubview:_sendButton];
    
    
    // 更多按钮
//    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    moreButton.viewSize = CGSizeMake(28, 28);
//    moreButton.right = self.width - 16;
//    moreButton.centerY = _inputTextView.centerY;
//    [moreButton setImage:[UIImage imageNamed:@"moreFunction"] forState:UIControlStateNormal];
//    [moreButton setImage:[UIImage imageNamed:@"moreFunction_selected"] forState:UIControlStateSelected];
//    [self addSubview:moreButton];
//    _moreButton = moreButton;
//
//    UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    emojiButton.frame = CGRectMake(0, 0, 28, 28);
//    emojiButton.right = moreButton.left - 10;
//    emojiButton.centerY = moreButton.centerY;
//    emojiButton.hidden = YES;
//    [emojiButton setImage:[UIImage imageNamed:@"emoji"] forState:UIControlStateNormal];
//    [emojiButton setImage:[UIImage imageNamed:@"emoji_selected"] forState:UIControlStateSelected];
//    [self addSubview:emojiButton];
//    self.emojiButton = emojiButton;

//    UIButton *realVoiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    realVoiceBtn.viewSize = CGSizeMake(24, 24);
//    [self addSubview:realVoiceBtn];
//    self.realVoiceButton = realVoiceBtn;
    
    UIButton *recordVoiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    recordVoiceButton.frame = self.inputTextView.frame;
    recordVoiceButton.layer.cornerRadius = 16;
    recordVoiceButton.layer.masksToBounds = YES;
    [recordVoiceButton setBackgroundImage:[UIImage qmui_imageWithColor:[UIColor colorWithRed:245/255.0 green:246/255.0 blue:248/255.0 alpha:1.0]] forState:UIControlStateNormal];
    [recordVoiceButton setTitle:@"按住说话"
                       forState:UIControlStateNormal];
    [recordVoiceButton setTitle:@"松手发送"
                       forState:UIControlStateHighlighted];
    [recordVoiceButton setTitleColor:[UIColor colorWithRed:137/255.0 green:144/255.0 blue:163/255.0 alpha:1.0]
                            forState:UIControlStateNormal];

    recordVoiceButton.titleLabel.font = [UIFont systemFontOfSize:12];
    recordVoiceButton.hidden = YES;
    [self addSubview:recordVoiceButton];
    self.recordVoiceButton = recordVoiceButton;
    
//    UIButton *audioRecognizeButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    audioRecognizeButton.viewSize = CGSizeMake(40, 40);
//    audioRecognizeButton.center = self.sendButton.center;
//    audioRecognizeButton.hidden = YES;
//    [audioRecognizeButton setImage:[UIImage imageNamed:@"imAudioRecognize"] forState:UIControlStateNormal];
//    [audioRecognizeButton setImage:[UIImage imageNamed:@"imCancelAudioReco"] forState:UIControlStateSelected];
//    [self addSubview:audioRecognizeButton];
//    self.audioRecognizeButton = audioRecognizeButton;
    
//    UILabel *recordVoiceLabel = [UILabel.alloc init];
//    recordVoiceLabel.frame = _inputTextView.frame;
//    recordVoiceLabel.layer.cornerRadius = 20;
//    recordVoiceLabel.layer.masksToBounds = YES;
//    recordVoiceLabel.backgroundColor = [UIColor colorWithHex:0xF9F9F9];
//    recordVoiceLabel.text = @"按住说话";
//    recordVoiceLabel.textColor = [UIColor colorWithHex:0x666666];
//    recordVoiceLabel.font = [UIFont systemFontOfSize:16];
//    recordVoiceLabel.hidden = YES;
//    [self addSubview:recordVoiceLabel];
//    self.recordVoiceLabel = recordVoiceLabel;
    
    // 切换录音按钮
    UIButton *voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    voiceButton.frame = CGRectMake(16, 8, 24, 24);
    [voiceButton setImage:[UIImage imageNamed:@"microphone"] forState:UIControlStateNormal];
    [voiceButton setImage:[UIImage imageNamed:@"keyboard"] forState:UIControlStateSelected];
    [self addSubview:voiceButton];
    _voiceButton = voiceButton;
    voiceButton.centerY = _inputTextView.centerY;
    
    [self addCustomButton];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _voiceButton.centerY = _inputTextView.centerY;
    _sendButton.centerY = _inputTextView.centerY;
    if (self.quote) {
        _quoteView.top = _inputTextView.bottom + 8;
        _quoteView.left = _inputTextView.left;
    }
}

- (CGSize)sizeThatFits:(CGSize)size
{
    UIEdgeInsets insets = UIEdgeInsetsMake(8, 16, 8, 16);
    return CGSizeMake(self.width, _inputTextView.height + insets.top + insets.bottom + (self.quote?_quoteView.height+9:0));
}

- (BOOL)showKeyboard
{
    return self.inputTextView.isFirstResponder;
}

- (void)setShowKeyboard:(BOOL)showKeyboard
{
    if (showKeyboard) {
        [self.inputTextView becomeFirstResponder];
    } else {
        [self.inputTextView resignFirstResponder];
    }
}

- (void)setContentText:(NSString *)contentText
{
    self.inputTextView.text = contentText;
    self.inputTextView.plainText = contentText;
}

- (NSString *)contentText
{
    return self.inputTextView.text;
}

#pragma mark - 公开

- (void)refreshStatus:(HLIMInputStatus)status
{
    self.status = status;
    [self sizeToFit];
    
    if (status == HLIMInputStatusText || status == HLIMInputStatusMore)
    {
        self.sendButton.hidden = NO;
        self.recordVoiceButton.hidden = YES;
        self.recordVoiceLabel.hidden = YES;
        self.inputTextView.hidden = NO;
        self.voiceButton.selected = NO;
    }
    else if(status == HLIMInputStatusAudio)
    {
        self.sendButton.hidden = NO;
        self.recordVoiceButton.hidden = NO;
        self.recordVoiceLabel.hidden = NO;
        self.inputTextView.hidden = YES;
        self.voiceButton.selected = YES;
    }
    else if (status == HLIMInputStatusEmoticon)
    {
        self.sendButton.hidden = NO;
        self.audioRecognizeButton.selected = NO;
        self.recordVoiceButton.hidden = YES;
        self.recordVoiceLabel.hidden = YES;
        self.inputTextView.hidden = NO;
        self.voiceButton.selected = NO;
    } else {
        self.sendButton.hidden = NO;
        self.recordVoiceButton.hidden = YES;
        self.recordVoiceLabel.hidden = YES;
        self.inputTextView.hidden = NO;
        self.voiceButton.selected = YES;
    }
}

- (void)addCustomButton
{
    
}

- (void)addCustomTarget:(id<HLIMKitInputView>)target
{
    
}
- (CGFloat)extraHeight
{
    return 0;
}

#pragma mark - NIMGrowingTextViewDelegate
- (BOOL)shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)replacementText
{
    BOOL should = YES;
    if ([self.delegate respondsToSelector:@selector(shouldChangeTextInRange:replacementText:)]) {
        should = [self.delegate shouldChangeTextInRange:range replacementText:replacementText];
    }
    return should;
}


- (BOOL)textViewShouldBeginEditing:(NIMGrowingTextView *)growingTextView
{
    BOOL should = YES;
    if ([self.delegate respondsToSelector:@selector(textViewShouldBeginEditing)]) {
        should = [self.delegate textViewShouldBeginEditing];
    }
    return should;
}

- (void)textViewDidEndEditing:(NIMGrowingTextView *)growingTextView
{
    if ([self.delegate respondsToSelector:@selector(textViewDidEndEditing)]) {
        [self.delegate textViewDidEndEditing];
    }
}


- (void)textViewDidChange:(NIMGrowingTextView *)growingTextView
{
    if ([self.delegate respondsToSelector:@selector(textViewDidChange)]) {
        [self.delegate textViewDidChange];
    }
}

- (void)willChangeHeight:(CGFloat)height
{
    _inputTextView.height = height;
    self.bg.height = height;
    
    UIEdgeInsets insets = UIEdgeInsetsMake(8, 16, 8, 16);
    CGFloat toolBarHeight = height + insets.top + insets.bottom;
    
    toolBarHeight += [self extraHeight];
    
    if (self.quote) {
        toolBarHeight += _quoteView.height;
    }
    
    if ([self.delegate respondsToSelector:@selector(toolBarWillChangeHeight:)]) {
        [self.delegate toolBarWillChangeHeight:toolBarHeight];
    }
}

- (void)didChangeHeight:(CGFloat)height
{
    _inputTextView.height = height;
    self.bg.height = height;
    
    UIEdgeInsets insets = UIEdgeInsetsMake(8, 16, 8, 16);
    self.height = height + insets.top + insets.bottom;
    
    self.height += [self extraHeight];
    
    if (self.quote) {
        self.height += _quoteView.height;
    }
    
    if ([self.delegate respondsToSelector:@selector(toolBarDidChangeHeight:)]) {
        [self.delegate toolBarDidChangeHeight:self.height];
    }
}

- (void)insertText:(NSString *)text
{
    if (!text || text.length == 0) {
        return;
    }
    NSRange range = self.inputTextView.selectedRange;
    NSString *replaceText = [self.inputTextView.text stringByReplacingCharactersInRange:range withString:text];
    range = NSMakeRange(range.location + text.length, 0);
    self.inputTextView.text = replaceText;
    self.inputTextView.selectedRange = range;
    
}

- (void)deleteText:(NSRange)range
{
    NSString *text = self.contentText;
    if (range.location + range.length <= [text length]
        && range.location != NSNotFound && range.length != 0)
    {
        NSString *newText = [text stringByReplacingCharactersInRange:range withString:@""];
        NSRange newSelectRange = NSMakeRange(range.location, 0);
        [self.inputTextView setText:newText];
        self.inputTextView.selectedRange = newSelectRange;
    }
}

- (void)insertQuote:(NSString *)quote
{
    self.quote = quote;
    
    if (!_quoteView) {
        _quoteView = [HLIMKitQuoteView.alloc initWithFrame:CGRectMake(0, 0, self.width - 64 - 88, 0)];
        _quoteView.delegate = self;
        [self addSubview:_quoteView];
    }
    _quoteView.message = quote;
    _quoteView.viewSize = [_quoteView sizeThatFits:CGSizeMake(self.width - 64 - 88, 0)];
    [_quoteView setNeedsLayout];
    [self sizeToFit];
    
    [self didChangeHeight:_inputTextView.height];
}

- (void)clearQuote
{
    self.quote = nil;
    [_quoteView removeFromSuperview];
    _quoteView = nil;
    [self sizeToFit];
    
    [self didChangeHeight:_inputTextView.height];
}

- (NSRange)selectedRange
{
    return self.inputTextView.selectedRange;
}

#pragma mark - 引用

- (void)imkit_quoteViewDidClose
{
    [self clearQuote];
}

@end
